//
//  QXSingleProductViewModel.swift
//  DanTangSwift
//
//  Created by 众力金融 on 2017/6/22.
//  Copyright © 2017年 ..... All rights reserved.
//

import UIKit
import SwiftyJSON

class QXSingleProductViewModel: NSObject {

    lazy var SingleProductItems = [QXSingleProductModel]()
    
    func singleProductData(success: @escaping (_ isSuccess: Bool) -> ()) {
        
        let url = BASE_URL + singleProduct_API
        let params = ["gender": 1,
                      "generation": 1,
                      "limit": 20,
                      "offset": 0]
        
        NetWorkTool.requestData(type: .get, urlString: url, parameters: params, succeed: { (result) in
            
            let dict = JSON(result)
             print(dict)
            let data = dict["data"].dictionary
            guard let items = data!["items"]?.arrayObject else {
                return
            }
            
            for item in items {
                let itemDict = item as! [String : AnyObject]
                if let itemData = itemDict["data"] {
                    let homeItem = QXSingleProductModel(dict: itemData as! [String : Any])
                    self.SingleProductItems.append(homeItem)
                }
            }
            
            print(self.SingleProductItems)
            success(true)
            
        }) { (err) in
            print(err)
             success(false)
        }
        
    }
    
}
